<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author QuangBV
 */
class Admin extends MY_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('admin_m', 'admin');

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
    }

    function index() {
        if($this->check_login()){
            //TODO
            
        }
    }


    public function login() {
        $data = array();
        echo 'please login !';
        $data['title'] = 'Please Login！';
        $data['error'] = '';
        if (!empty($_POST)) {
            $admin_email = trim($this->input->post('admin_email'));
            $admin_pass = trim($this->input->post('admin_pass'));
            $result = $this->admin->login($admin_email, $admin_pass);
            if (!$result)
                $data['error'] = 'Wrong username or password, please try again !';
        }
        if ($this->session->userdata('logged_in') != '')
            $data['title'] = '歓迎 !';
        $this->load->view("login_v", $data);
    }

    public function logout() {
        //check login
        $this->check_login();
        //end - check login
        $this->account_model->logout();
        redirect(base_url());
    }

    public function change_pass() {
        //check login
        $this->check_login();
        //end - check login
        $data = array();
        $data['title'] = 'change password';
        $data['user'] = $this->session->userdata('user');
        $data['error'] = '';
        $data['success'] = '';
        $data['active'] = 1;
        if (!empty($_POST)) {
            $admin_email = $this->session->userdata('user');
            $old_pass = trim($this->input->post('old_pass'));
            $new_pass = trim($this->input->post('new_pass'));
            $result = $this->account_model->change_pass($admin_email, $old_pass, $new_pass);
            if (!$result)
                $data['error'] = 'Wrong username or old password, please try again !';
            else
                $data['success'] = 'Change password success !';
        }
        $this->load->view("change_pass_v",$data);
    }

    public function create_new() {
        //check login
        $this->check_login();
        //end - check login
        $data = array();
        $data['title'] = '新しいアカウントを作成する:';
        $data['user'] = $this->session->userdata('user');
        $data['error'] = '';
        $data['success'] = '';
        $data['active'] = 1;

        if (!empty($_POST)) {
            $admin_email = trim($this->input->post('admin_email'));
            $admin_pass = trim($this->input->post('admin_pass'));
            $result = $this->account_model->create_new($admin_email, $admin_pass);
            if (!$result)
                $data['error'] = 'Error in creating account, please try again !';
            else
                $data['success'] = 'Create account success !';
        }
        $data['admin_list'] = $this->account_model->show_list();
        $this->load->template("create_admin_v", $data);
    }

    public function delete() {
        //check login
        $this->check_login();
        //end - check login
        $data = array();
        $data['title'] = 'Delete account';
        $data['user'] = $this->session->userdata('user');
        $data['error'] = '';
        $data['success'] = '';
        $data['active'] = 1;
        if (!empty($_GET)) {
            $admin_id = (int) trim($this->input->get('id'));
            if ($this->account_model->delete($admin_id))
                $data['success'] = 'Delete account success !';
            else
                $data['error'] = 'Error in deleting account, please try again !';
        }
        $data['admin_list'] = $this->account_model->show_list();
        $this->load->template("delete_admin_v", $data);
    }

}

?>
